import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartLiquidfillModel {
  /// 水球图底下显示的文本
  String nameText;

  /// 水球图底下显示的文本距离底部的距离
  double nameTextPaddingBottom;

  /// 水球图底下显示的文本的颜色
  Color nameTextColor;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 图表样式
  BaseChartStyle style;

  /// 图表数据
  List<double> data;

  /// 水球图样式
  BaseChartLiquidFillStyle liquidFillStyle;

  BaseChartLiquidfillModel({
    this.style,
    this.nameText,
    this.nameTextPaddingBottom = 20,
    this.nameTextColor = Colors.black,
    this.labelFormatter,
    this.liquidFillStyle,
    @required this.data,
  });

  factory BaseChartLiquidfillModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    return BaseChartLiquidfillModel(
        nameText: asT<String>(jsonRes['nameText']),
        nameTextPaddingBottom: asT<double>(jsonRes['nameTextPaddingBottom']),
        nameTextColor: asT<Color>(jsonRes['nameTextColor']),
        labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
        style: asT<BaseChartStyle>(jsonRes['style']),
        liquidFillStyle:
            asT<BaseChartLiquidFillStyle>(jsonRes['liquidFillStyle']),
        data: asT<List<double>>(jsonRes['data']));
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'nameText': nameText,
        'nameTextPaddingBottom': nameTextPaddingBottom,
        'nameTextColor': nameTextColor,
        'labelFormatter': labelFormatter,
        'style': style,
        'liquidFillStyle': liquidFillStyle,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartLiquidFillStyle {
  /// 水的颜色
  List<String> color;
  bool waveAnimation;
  double animationDuration;
  double animationDurationUpdate;
  BaseChartLiquidFillStyleOutline outline;
  BaseChartLiquidFillStyleBackgroundStyle backgroundStyle;

  BaseChartLiquidFillStyle(
      {this.color,
      this.outline,
      this.backgroundStyle,
      this.waveAnimation = true,
      this.animationDuration = 2000,
      this.animationDurationUpdate = 1000});

  Map<String, dynamic> toJson() {
    return {
      'color': color ?? ['#EC724B'],
      'waveAnimation': waveAnimation,
      'animationDuration': animationDuration,
      'animationDurationUpdate': animationDurationUpdate,
      'outline':
          outline?.toJson() ?? BaseChartLiquidFillStyleOutline().toJson(),
      'backgroundStyle': backgroundStyle?.toJson() ??
          BaseChartLiquidFillStyleBackgroundStyle().toJson()
    };
  }
}

class BaseChartLiquidFillStyleOutline {
  bool show;
  double borderDistance;
  BaseChartLiquidFillStyleOutlineItemStyle itemStyle;

  BaseChartLiquidFillStyleOutline(
      {this.show = true, this.borderDistance = 4, this.itemStyle});

  Map<String, dynamic> toJson() {
    return {
      'show': show,
      'borderDistance': borderDistance,
      'itemStyle': itemStyle?.toJson() ??
          BaseChartLiquidFillStyleOutlineItemStyle().toJson()
    };
  }
}

class BaseChartLiquidFillStyleOutlineItemStyle {
  String borderColor;
  String shadowColor;
  double borderWidth;
  double shadowBlur;

  BaseChartLiquidFillStyleOutlineItemStyle(
      {this.borderColor = '#EC724B',
      this.shadowColor = 'white',
      this.borderWidth = 1,
      this.shadowBlur = 0});

  Map<String, dynamic> toJson() {
    return {
      'borderColor': borderColor,
      'shadowColor': shadowColor,
      'borderWidth': borderWidth,
      'shadowBlur': shadowBlur
    };
  }
}

class BaseChartLiquidFillStyleBackgroundStyle {
  String color;
  String borderColor;
  double borderWidth;
  BaseChartLiquidFillStyleBackgroundStyleItemStyle itemStyle;

  BaseChartLiquidFillStyleBackgroundStyle(
      {this.color = '#EC724B',
      this.borderColor = '#EC724B',
      this.borderWidth = 1,
      this.itemStyle});

  Map<String, dynamic> toJson() {
    return {
      'color': color,
      'borderColor': borderColor,
      'borderWidth': borderWidth,
      'itemStyle': itemStyle?.toJson() ??
          BaseChartLiquidFillStyleBackgroundStyleItemStyle().toJson()
    };
  }
}

class BaseChartLiquidFillStyleBackgroundStyleItemStyle {
  double shadowBlur;
  double opacity;
  String shadowColor;

  BaseChartLiquidFillStyleBackgroundStyleItemStyle(
      {this.shadowBlur = 0, this.opacity = 1, this.shadowColor = 'white'});

  Map<String, dynamic> toJson() {
    return {
      'shadowBlur': shadowBlur,
      'opacity': opacity,
      'shadowColor': shadowColor,
    };
  }
}
